package com.gxa.meiye.admin.shop.controller;

import com.gxa.meiye.admin.shop.service.EditPwdService;
import com.gxa.meiye.admin.shop.service.LoginService;
import com.gxa.meiye.core.common.JsonResult;
import com.gxa.meiye.core.common.ResponseData;
import com.gxa.meiye.core.pojo.shopManagement.Shop;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.crypto.hash.SimpleHash;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

/**
 * @Author cc
 * @PackageName meiye
 * @Package com.gxa.meiye.admin.shop.controller
 * @Date 2022/7/22 14:59
 * @Version 1.0
 */
@RestController
@RequestMapping("shop")
public class AdminLoginController {
    @Autowired
    private LoginService loginService;
    @Autowired
    private EditPwdService editPwdService;
    @ApiOperation("前台调用登录的密码和邮箱")
    @PostMapping("shopLogin")
    public Map login(Shop shop){
        String email = shop.getEmail();
        String pwd = shop.getPwd();
        JsonResult jsonResult = new JsonResult();
        SimpleHash simpleHash = new SimpleHash("MD5",pwd,email,1024);
        String MD5pwd = simpleHash.toString();
        jsonResult.setData(loginService.queryBuyEmailAndPwd(email, MD5pwd));
        return jsonResult.getMap();
    }

    @ApiOperation("前台调用设置新密码")
    @PostMapping("setNewPwd")
    public ResponseData setNewPwd(Shop shop){
        ResponseData rd = new ResponseData();
        String pwd = shop.getPwd();
        String email = shop.getEmail();
        SimpleHash simpleHash = new SimpleHash("MD5",pwd,email,1024);
        String MD5pwd = simpleHash.toString();
        shop.setPwd(MD5pwd);
        editPwdService.editPwdByEmail(shop);
        rd.setCode(200);
        rd.setMsg("修改密码成功");
        return rd;
    }

}
